/******************************************************************************** /
 * EntryInputDialog.java - JPLearning Project									*
 * 																				*
 * 		This program is free software; you can redistribute it and/or modify	*
 *		it under the terms of the GNU General Public License as published by	*
 *		the Free Software Foundation; version 2 of the License.					*
 *																				*
 *		This program is distributed in the hope that it will be useful,			*
 *		but WITHOUT ANY WARRANTY; without even the implied warranty of			*
 *		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			*
 *		GNU General Public License for more details.							*
 *																				*
 ********************************************************************************/
package org.jplearning.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import org.jplearning.common.Constants;
import org.jplearning.model.WordEntry;

public class EntryInputDialog extends JDialog{
	private JPanel mainPanel;
	private JPanel centralPanel;
	private JPanel botonesPanel;
	
	private JComboBox tipoPalabra;
	private JButton buttonAceptar;
	private JButton buttonCancelar;
	
	private boolean canceled;
	
	public EntryInputDialog() {
		super();
		initialize();
		setModal(true);
		pack();
	}
	
	public void initialize(){
		getContentPane().add(getMainPanel());
	}
	
	public JPanel getMainPanel(){
		if(mainPanel == null){
			mainPanel = new JPanel(new BorderLayout());
			mainPanel.add(getCentralPanel(), BorderLayout.CENTER);
			mainPanel.add(getBotonesPanel(), BorderLayout.SOUTH);
		}
		return mainPanel;
	}
	
	public JPanel getCentralPanel(){
		if(centralPanel == null){
			centralPanel = new JPanel();
			centralPanel.add(getTipoPalabra());
		}
		return centralPanel;
	}
	
	public JPanel getBotonesPanel(){
		if(botonesPanel == null){
			botonesPanel = new JPanel();
			botonesPanel.add(getButtonAceptar());
			botonesPanel.add(getButtonCancelar());
		}
		return botonesPanel;
	}
	
	public JButton getButtonAceptar(){
		if(buttonAceptar == null){
			buttonAceptar = new JButton("Aceptar");
			buttonAceptar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					buttonAceptar_actionPerformed(e);
				}
			});
		}
		return buttonAceptar;
	}

	private void buttonAceptar_actionPerformed(ActionEvent e) {
		setVisible(false);
	}

	public JButton getButtonCancelar(){
		if(buttonCancelar == null){
			buttonCancelar = new JButton("Cancelar");
			buttonCancelar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					buttonCancelar_actionPerformed(e);
				}
			});
		}
		return buttonCancelar;
	}
	
	private void buttonCancelar_actionPerformed(ActionEvent e) {
		this.canceled = true;
		setVisible(false);
	}

	public JComboBox getTipoPalabra(){
		if(tipoPalabra == null){
			DefaultComboBoxModel model = new DefaultComboBoxModel();
			model.addElement(WordEntry.ADJETIVO);
			model.addElement(WordEntry.ADVERBIO);
			model.addElement(WordEntry.NOMBRE);
			model.addElement(WordEntry.PARTICULA);
			model.addElement(WordEntry.VERBO);

			tipoPalabra = new JComboBox(model);
		}
		return tipoPalabra;
	}
	
	public WordEntry getWordEntry(){
		if(canceled) return null;
		String tipoPalabra = (String)getTipoPalabra().getSelectedItem();
		WordEntry entry = new WordEntry(tipoPalabra);
		return entry;
	}
}
